/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.adapter.protocol.chain;

import com.cetccloud.cimp.adapter.handler.*;
import com.cetccloud.cimp.adapter.handler.abstracts.*;
import com.cetccloud.cimp.adapter.protocol.ProtocolExecutor;
import com.cetccloud.cimp.adapter.util.SpringAppContextUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import lombok.Data;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @apiNote 双向处理链路初始化
 * @date 2022/11/16 10:25
 */
@Component
@Data
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DefaultDataChain {
    AbstractInnerMQ innerMQ;
//    AbstractProtocolEndpoint protocolEndpoint;
    AbstractAuthHandler authHandler;
//    AbstractParserHandler parserHandler;
//    AbstractCheckHandler checkHandler;
    private final static InternalLogger logger = InternalLoggerFactory.getInstance(DefaultDataChain.class);

    private Map<String,Object> handlerMap = new HashMap<>();

    /**
     * 设置出入操作执行链路
     */
    public void makeDataChain(AbstractAuthHandler authHandler,
                              AbstractParserHandler parserHandler,
                              AbstractCheckHandler checkHandler,
                              ProtocolExecutor protocolExecutor) {
        logger.info("初始化出站入站执行链路！！！");
        innerMQ = SpringAppContextUtil.getBean(DefaultInnerMQHandler.class);
        
        this.authHandler = authHandler;

        handlerMap.put(SpringAppContextUtil.getApplicationContext().getBeanNamesForType(authHandler.getClass())[0],authHandler);
        handlerMap.put(SpringAppContextUtil.getApplicationContext().getBeanNamesForType(parserHandler.getClass())[0],parserHandler);
        handlerMap.put(SpringAppContextUtil.getApplicationContext().getBeanNamesForType(checkHandler.getClass())[0],checkHandler);


        //入站数据链路
        authHandler.setNextFromDtuHandler(parserHandler);
        parserHandler.setNextFromDtuHandler(checkHandler);
        checkHandler.setNextFromDtuHandler(innerMQ);

        //出站数据链路
        innerMQ.setNextToDtuHandler(checkHandler);
        checkHandler.setNextToDtuHandler(parserHandler);
        parserHandler.setNextToDtuHandler(authHandler);
        authHandler.setNextToDtuHandler(protocolExecutor);
    }
}
